//
//  SmallP_ex_scrollView.swift
//  SmallPrograms
//
//  Created by kadenJack on 2022/9/9.
//

import UIKit

//MARK: - UITableView
extension UITableView {
    
    //TODO: - register
    func register(nib cells: UITableViewCell.Type..., in bundle: Bundle? = .main) {
        cells.forEach({ register(UINib(nibName: String(describing: $0), bundle: bundle), forCellReuseIdentifier: String(describing: $0)) })
    }
    
    func register(_ cells: UITableViewCell.Type...) {
        cells.forEach({ register($0.self, forCellReuseIdentifier: String(describing: $0)) })
    }
    
    //TODO: -
    func cell<T: UITableViewCell>(_ type: T.Type, indexPath: IndexPath) -> T {
        guard let _ = dequeueReusableCell(withIdentifier: String(describing: type)) else {
            register(type)
            return cell(type, indexPath: indexPath)
        }
        guard let cell = dequeueReusableCell(withIdentifier: String(describing: type), for: indexPath) as? T else {
            fatalError(
                "没有注册此Cell: \(String(describing: type))")
        }
        return cell
    }
    
}


extension UICollectionView {
    
    //TODO: - register
    func register(nib cells: UICollectionViewCell.Type..., in bundle: Bundle? = .main) {
        cells.forEach({ register(UINib(nibName: String(describing: $0), bundle: bundle), forCellWithReuseIdentifier: String(describing: $0)) })
    }
    
    func register(_ cells: UICollectionViewCell.Type...) {
        cells.forEach({ register($0.self, forCellWithReuseIdentifier: String(describing: $0)) })
    }
    
    //TODO: -
    func cell<T: UICollectionViewCell>(_ type: T.Type, indexPath: IndexPath) -> T {
        guard let cell = dequeueReusableCell(withReuseIdentifier: String(describing: type), for: indexPath) as? T else {
            // fatalError("没有注册此Cell: \(String(describing: type))")
            register(type)
            return cell(type, indexPath: indexPath)
        }
        return cell
    }
    
}
